package com.ly.cloud.service.impl;

import com.ly.cloud.exception.CloudException;
import com.ly.cloud.vo.NjSelectVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.NjDto;
import com.ly.cloud.entity.NjPo;
import com.ly.cloud.entity.XqPo;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.NjMapper;
import com.ly.cloud.service.NjService;
import com.ly.cloud.vo.NjVo;

import java.util.List;
import java.util.Map;

/**
 * 年级信息实现类
 * 
 * @author siqi
 * @date 2018-12-13
 */
@Service
public class NjServiceImpl implements NjService {

	@Autowired
	public NjMapper njMapper;

	/**
	 * 分页查询
	 */
	@Override
	public Page<NjVo> queryNj(Page<NjVo> page, String njmc) {
		try {
			return page.setRecords(njMapper.queryNj(page, njmc));
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败", e);
		}
	}

	/**
	 * 新增年级
	 */
	@Override
	public boolean insertNj(NjDto njDto) {
		try {
			NjPo njPo = new NjPo();
			BeanUtils.copyProperties(njDto, njPo);
			Integer rs = njMapper.insert(njPo);
			if (rs > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("新增失败", e);
		}
	}

	/**
	 * 修改年级信息
	 */
	@Override
	public boolean updateNj(NjDto njDto) {
		try {
			NjPo njPo = new NjPo();
			BeanUtils.copyProperties(njDto, njPo);
			EntityWrapper<NjPo> ew = new EntityWrapper<>();
			ew.where("njdm={0}", njPo.getNjdm());
			Integer rs = njMapper.update(njPo, ew);
			if (rs > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("更新失败", e);
		}
	}

	/**
	* @Description: 查询所有年级，下拉通用
	* @Author: pengshiqing
	* @CreateDate: 2018/12/20
	*/
    @Override
    public List<NjSelectVo> findAllNj() {
        try {
            return njMapper.findAllNj();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询所有年级失败!", e);
        }
    }

}
